-- Thanks:https://github.com/catgoose/nvim
-- Just used as winbar

local b = require('utils.buf')
local w = require('utils.win')
local m = require('utils.color')
local t = require('utils.table')

local config = function()
    local heirline = require("heirline")
    local conditions = require("heirline.conditions")
    local u = require("heirline.utils")

    local fn, api, bo = vim.fn, vim.api, vim.bo
    local winbar_bg = m.bg
    local file_name_fg = m.yellow_dark
    local file_name_bg = m.bg_cursorline
    local winbar_inactive = {
        buftype = { "nofile","prompt", "quickfix", "terminal","acwrite"},
        filetype = { "toggleterm", "qf", "terminal", "gypsy","neo-tree" },
    }
    local Align = { provider = "%=" }
    local Space = { provider = " " }
    local SepLeft = { provider = "" }

    local ActiveWindow = {
        hl = function()
            return { bg = winbar_bg }
        end,
    }
    local ActiveBlock = {
        hl = function()
            return { bg = file_name_bg }
        end,
    }
    local ActiveSep = {
        hl = function()
            return { fg = winbar_bg }
        end,
    }
    local FileName = {
        provider = function(self)
            local filename = fn.fnamemodify(self.filename, ":t")
            if filename == "" then
                return ""
            end
            local path = fn.fnamemodify(self.filename, ":h")  -- 获取路径部分
            local parts = vim.split(path, "/")                -- 分割成数组
            local n = #parts
            local last_path_part = ""
            if n >= 2 then
                last_path_part = parts[n-1] .. "/" .. parts[n]
            elseif n == 1 then
                last_path_part = parts[1]
            end -- get last two parts of path
            return string.format("%%#HeirlinePath#%s/%%#HeirlineFileName#%s", last_path_part, filename)
        end,
    }
    local ErrorFlag = {
        {
            provider = function()
                local buffers = b.get_listed_bufs()
                local dn = 0
                t.foreach(buffers,function(buf)
                    local d = #vim.diagnostic.get(buf, {severity = vim.diagnostic.severity['ERROR']})
                    if d > 0 then
                        dn = dn + 1
                    end
                end)
                local has_qf = w.has_quickfix_win()
                if has_qf or dn > 0 then
                    return '  '
                end
                return ''
            end,
            hl = { fg = m.red_deep },
        },
    }
    local FileFlags = {
        {
            provider = function()
                local buffers = b.get_listed_bufs()
                local n = 0
                local ms = {}
                t.foreach(buffers,function(buf)
                    if b.is_modified(buf) then
                        t.append(ms, buf);
                        n = n + 1
                    end
                end)
                vim.g.divo_not_save_buffers = ms

                if n == 0 then
                    return ''
                end
                local c = ''
                if bo.modified then
                    if n == 1 then
                        c = "[+] "
                    else
                        c = "[+][" .. (n-1) .. "] "
                    end
                else
                    c = "[" .. n .. "] "
                end
                return c
            end,
        },
    }
    local FileNameModifer = {
        hl = function()
            if bo.modified then
                return { italic = true, force = true }
            end
        end,
    }
    local FileNameBlock = {
        init = function(self)
            self.filename = api.nvim_buf_get_name(0)
        end,
    }
    FileNameBlock = u.insert(
        FileNameBlock,
        u.insert(ActiveSep, SepLeft),
        Space,
        unpack(ErrorFlag),
        unpack(FileFlags),
        u.insert(FileNameModifer, FileName, Space),
        { provider = "%<" }
    )
    ActiveWinbar = {
        condition = function()
            return not conditions.buffer_matches(winbar_inactive)
        end,
        Align,
        u.insert(ActiveBlock, FileNameBlock),
    }

    local WinBars = {
        u.insert(ActiveWindow, ActiveWinbar),
    }

    heirline.setup({
        winbar = WinBars,
        opts = {
            disable_winbar_cb = function(args)
                return conditions.buffer_matches(winbar_inactive, args.buf)
            end,
        },
    })
    vim.opt.winbar = "%{%v:lua.require'heirline'.eval_winbar()%}"
end

return {
    "rebelot/heirline.nvim",
    config = config,
    event = "BufReadPre",
    dependencies = {
        {"nvim-tree/nvim-web-devicons", pin=true},
    },
}
-- EOP
